package com.niannian.adapter;

import java.util.ArrayList;
import java.util.Date;

import com.niannian.R;
import com.niannian.activity.FamilyMemberDetailActivity;
import com.niannian.activity.MyInfoEditActivity;
import com.niannian.activity.PushRegardsActivity;
import com.niannian.activity.RegardsSendActivity;
import com.niannian.bean.GreetingNewsBean;
import com.niannian.bean.MyFamilyMemberBean;
import com.niannian.db.MyDBUser;
import com.niannian.dialog.YesNoDialog2;
import com.niannian.dialog.YesNoDialog2.onSimpleOnCancelListener;
import com.niannian.dialog.YesNoDialog2.onSimpleOnClicklListener;
import com.niannian.util.Common;
import com.niannian.util.PlayerManager;
import com.niannian.util.UserInfoManager;
import com.niannian.util.VAR;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 发送的问候的adapter
 * 
 * @author shenglin
 * 
 */
public class RegardsSendAdapter extends BaseAdapter {
	protected MyDBUser userInfoManager = UserInfoManager.getUserInfoInstance();
	private ArrayList<GreetingNewsBean> fdatas;
	private LayoutInflater inflater;
	private Activity mActivity;

	private Handler handler;
	BaseAdapter adapter;

	public RegardsSendAdapter(Activity activity, Handler handler) {
		super();
		this.mActivity = activity;
		this.handler = handler;
		inflater = LayoutInflater.from(mActivity);
		this.fdatas = new ArrayList<GreetingNewsBean>();
	}

	@Override
	public int getCount() {
		return fdatas.size();
	}

	@Override
	public Object getItem(int arg0) {
		return fdatas.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	public ArrayList<GreetingNewsBean> getFDatasList() {
		return fdatas;
	}

	public void setAdapter(BaseAdapter adapter) {
		this.adapter = adapter;
	}

	AnimationDrawable ad;

	@Override
	public View getView(final int position, View view, ViewGroup arg2) {
		ViewHolder holder = null;
		if (view == null) {
			view = inflater.inflate(R.layout.regards_send_itme, null);
			holder = new ViewHolder();
			holder.ll_itme_head = (LinearLayout) view.findViewById(R.id.ll_itme_head);
			holder.tv_greeting_send_user_name = (TextView) view.findViewById(R.id.tv_greeting_send_user_name);
			holder.tv_greeting_send_time = (TextView) view.findViewById(R.id.tv_greeting_send_time);
			holder.tv_greeting_send_info = (TextView) view.findViewById(R.id.tv_greeting_send_info);
		
			holder.tv_aud_len = (TextView) view.findViewById(R.id.tv_aud_len);
			holder.btn_play = (RelativeLayout) view.findViewById(R.id.btn_play);
			holder.iv_status_play = (ImageView) view.findViewById(R.id.iv_status_play);

			holder.pb_fn_loading = (ProgressBar) view.findViewById(R.id.pb_fn_loading);

			holder.tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
			holder.tv_afresh_edit = (TextView) view.findViewById(R.id.tv_afresh_edit);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		final GreetingNewsBean fdata = fdatas.get(position);
		holder.tv_greeting_send_user_name.setText(fdata.getReceiver_nickname());
		
		holder.tv_aud_len.setText(Common.showAudLen(fdata.getAud_len()));

		int curtime = (int) ((new Date()).getTime() / 1000);

		int time = (Integer) curtime - fdata.getSent_time();
		if (time < 0) {
			holder.tv_cancel.setVisibility(View.VISIBLE);
			holder.tv_afresh_edit.setVisibility(View.VISIBLE);
		} else {
			holder.tv_cancel.setVisibility(View.GONE);
			holder.tv_afresh_edit.setVisibility(View.GONE);
		}

		// 播放视图显示

		if (fdata.getAud_len()>0) {
			holder.btn_play.setVisibility(View.VISIBLE);
			if (PlayerManager.getAudioPlayer(mActivity).currPlayingPath.equals(fdata.getAud())) {
				if (PlayerManager.getAudioPlayer(mActivity).isPlayStatus == 1) {
					holder.iv_status_play.setBackgroundResource(R.drawable.family_status_play_anim);
					ad = (AnimationDrawable) holder.iv_status_play.getBackground();
					holder.iv_status_play.getViewTreeObserver().addOnPreDrawListener(opdl);
				} else if (PlayerManager.getAudioPlayer(mActivity).isPlayStatus == 2) {
					holder.iv_status_play.setBackgroundResource(R.drawable.status_pause);
				} else {
					holder.iv_status_play.setBackgroundResource(R.drawable.play_3);
				}
			} else {
				holder.iv_status_play.setBackgroundResource(R.drawable.play_3);
			}
		}else {
			holder.btn_play.setVisibility(View.GONE);
		}
		
		

		holder.tv_greeting_send_time.setText(Common.sgmdate3(fdata.getSent_time(), holder.ll_itme_head, holder.tv_greeting_send_time));

		if (!Common.empty(fdata.getInfo())) {
			holder.tv_greeting_send_info.setVisibility(View.VISIBLE);
			holder.tv_greeting_send_info.setText(fdata.getInfo());
		} else {
			holder.tv_greeting_send_info.setVisibility(View.GONE);
		}

		if (PlayerManager.getAudioPlayer(mActivity).DownloadingMap.containsKey(fdata.getAud())) {
			holder.pb_fn_loading.setVisibility(View.VISIBLE);
		} else {
			holder.pb_fn_loading.setVisibility(View.GONE);
		}

		holder.btn_play.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (System.currentTimeMillis() - VAR.ExitTime > VAR.INTERVAL) {
					PlayerManager.getAudioPlayer(mActivity).aPlayer(adapter, fdata.getAud());
					VAR.ExitTime = System.currentTimeMillis();
				}
			}
		});

		holder.tv_greeting_send_user_name.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (userInfoManager.id == fdata.getReceiver()) {
					Intent intent = new Intent(mActivity, MyInfoEditActivity.class);
					mActivity.startActivity(intent);
					mActivity.overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
				} else {
					MyFamilyMemberBean fd = new MyFamilyMemberBean();
					fd.setId(fdata.getReceiver());
					// fd.setFamily(fdata.getFamily());
					fd.setNickname(fdata.getReceiver_nickname());

					Intent intent = new Intent(mActivity, FamilyMemberDetailActivity.class);
					intent.putExtra("position", position);
					intent.putExtra("data", fd);
					mActivity.startActivity(intent);
					mActivity.overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
				}
			}
		});
		holder.ll_itme_head.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
			}
		});

		holder.tv_afresh_edit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				Bundle pBundle = new Bundle();
				pBundle.putSerializable("fdata", fdata);
				pBundle.putInt("from", PushRegardsActivity.FROM_EDIT_REGARDS);
				Intent data = new Intent(mActivity, PushRegardsActivity.class);
				data.putExtras(pBundle);
				mActivity.startActivityForResult(data, RegardsSendActivity.EDIT_SEND_REGARDS);
			}
		});
		holder.tv_cancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				curView = (View) v.getParent().getParent();
				initDialog(fdata.getId(), position);
			}
		});

		if (fdata.getIsdelete() == 1 && curView != null) {
			removeListItem(curView, position);
		}
		return view;
	}

	View curView;
	// 删除动态提示
	YesNoDialog2 tip;

	private void initDialog(final int id, final int position) {
		tip = new YesNoDialog2(mActivity, "确认要撤销吗?");
		tip.setOnCancelListener(new onSimpleOnCancelListener() {

			@Override
			public void onCancel() {
				tip.dismiss();
			}
		});
		tip.setOnConfimClickListener(new onSimpleOnClicklListener() {

			@Override
			public void onClick() {
				Message msg = new Message();
				msg.what = RegardsSendActivity.DELETE_SEND_REGARDS;
				msg.arg1 = id;
				msg.arg2 = position;
				handler.sendMessage(msg);
				tip.dismiss();
				// removeListItem(curView, position);
			}
		});
		tip.show();
	}

	protected void removeListItem(View rowView, final int positon) {
		final Animation animation = (Animation) AnimationUtils.loadAnimation(rowView.getContext(), R.anim.item_anim);
		animation.setAnimationListener(new AnimationListener() {
			public void onAnimationStart(Animation animation) {
			}

			public void onAnimationRepeat(Animation animation) {
			}

			public void onAnimationEnd(Animation animation) {
				fdatas.remove(positon);
				notifyDataSetChanged();
				animation.cancel();
			}
		});

		rowView.startAnimation(animation);
	}

	OnPreDrawListener opdl = new OnPreDrawListener() {
		@Override
		public boolean onPreDraw() {
			ad.start();
			return true;
		}
	};

	class ViewHolder {
		LinearLayout ll_itme_head;
		TextView tv_greeting_send_user_name;
		TextView tv_greeting_send_time;
		TextView tv_greeting_send_info;
		ProgressBar pb_fn_loading;

		TextView tv_aud_len;
		RelativeLayout btn_play;
		ImageView iv_status_play;

		TextView tv_cancel, tv_afresh_edit;
	}

}
